//
//  ShareViewController.swift
//  TestHHHHH
//
//  Created by 刘汤圆 on 2018/8/22.
//  Copyright © 2018年 刘汤圆. All rights reserved.
//

import UIKit
import SnapKit

class TopImgBottomTitleButton: UIButton {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        var frame: CGRect! = imageView!.frame
        let imageHeight: CGFloat = frame.size.height
        frame = CGRect(x: (bounds.size.width - frame.size.width) / 2, y: (bounds.size.height - imageHeight) / 2.0 - 6.0, width: frame.size.width, height: imageHeight)
        imageView!.frame = frame!
        frame = titleLabel!.frame
        titleLabel?.textAlignment = .center
        frame = CGRect(x: 0, y: (imageView?.bounds.size.height)! + 30, width: bounds.size.width, height: frame?.size.height ?? 0.0)
        titleLabel?.frame = frame!
    }
}

public typealias ShareClickToPlatform = (_ btn: UIButton) -> Void

open class ShareViewController: UIViewController {

    var containView: UIView!
    var closeBtn: UIButton!
    var verticalLine: UIView!
    
    var shareClickClosure:ShareClickToPlatform = {_ in}
    
    override open func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3)
        setupUI()
    }
    
    // MARK: set ui
    func setupUI() {
        
        containView = UIView()
        containView.layer.masksToBounds = true
        containView.layer.cornerRadius = 8
        containView.backgroundColor = .white
        containView.center = self.view.center
        self.view.addSubview(containView)
        containView.snp.makeConstraints { (make) in
            make.height.width.equalTo(300)
            make.centerX.equalTo(self.view.snp.centerX)
            make.centerY.equalTo(self.view.snp.centerY)
        }
        
        let titleLabel = UILabel()
        titleLabel.font = UIFont.systemFont(ofSize: 16)
        titleLabel.text = "分享到"
        titleLabel.textColor = UIColor(red: 74/255.0, green: 74/255.0, blue: 74/255.0, alpha: 1)
        titleLabel.textAlignment = .center
        containView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(containView.snp.centerX)
            make.top.equalTo(containView.snp.top).offset(15)
            make.height.equalTo(22)
        }
        
        let seprator = UIView()
        seprator.backgroundColor = UIColor(red: 236/255.0, green: 236/255.0, blue: 236/255.0, alpha: 1)
        containView.addSubview(seprator)
        seprator.snp.makeConstraints { (make) in
            make.left.equalTo(containView.snp.left).offset(4)
            make.right.equalTo(containView.snp.right).offset(-4)
            make.top.equalTo(titleLabel.snp.bottom).offset(15)
            make.height.equalTo(1)
        }
        
        let w = 100
        let h = 100
        let titleArray = ["微信好友", "微信朋友圈", "QQ好友", "QQ空间"]
        let imgArray = ["WeChat", "Moments", "QQ", "QZone"]
        for i in 0...3 {
            let btn = TopImgBottomTitleButton(type: .custom)
            btn.setImage(UIImage(named: imgArray[i]), for: .normal)
            btn.setTitle(titleArray[i], for: .normal)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            btn.setTitleColor(UIColor(red: 74/255.0, green: 74/255.0, blue: 74/255.0, alpha: 1), for: .normal)
            btn.tag = i
            btn.addTarget(self, action: #selector(clickAct(btn:)), for: .touchUpInside)
            containView.addSubview(btn)
            btn.snp.makeConstraints { (make) in
                make.height.equalTo(h)
                make.width.equalTo(w)
                make.left.equalTo(containView.snp.left).offset(i % 3 * w)
                make.top.equalTo(seprator.snp.bottom).offset(i != 3 ? 5 : 115)
            }
        }
        
        verticalLine = UIView()
        verticalLine.isHidden = true
        verticalLine.backgroundColor = .white
        self.view.addSubview(verticalLine)
        verticalLine.snp.makeConstraints { (make) in
            make.height.equalTo(60)
            make.width.equalTo(1)
            make.centerX.equalTo(self.view.snp.centerX)
            make.top.equalTo(containView.snp.bottom)
        }
        
        closeBtn = UIButton(type: .custom)
        closeBtn.setImage(UIImage(named: "Close"), for: .normal)
        closeBtn.addTarget(self, action: #selector(closeAct), for: .touchUpInside)
        self.view.addSubview(closeBtn)
        closeBtn.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp.centerX)
            make.width.height.equalTo(42)
            make.top.equalTo(verticalLine.snp.bottom)
        }
        
        containView.transform = CGAffineTransform(translationX: 0, y: (self.view.bounds.size.height / 2.0 + 150))
        verticalLine.transform = CGAffineTransform(translationX: 0, y: -60)
        closeBtn.transform = CGAffineTransform(translationX: 0, y: self.view.bounds.size.height / 2.0 - 150 - 60)
        
        animation()
    }
    
    func  animation() {
        
        UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
            self.containView.transform = .identity
        }) { (finish) in
            self.verticalLine.isHidden = false
            UIView.animate(withDuration: 0.25, animations: {
                self.verticalLine.transform = .identity
                self.closeBtn.transform = .identity
            })
        }
    }
    
    @objc func clickAct(btn: UIButton) {
        
        self.shareClickClosure(btn)
    }

    @objc func closeAct() {
        
        guard parent != nil else { return }
        willMove(toParentViewController: nil)
        removeFromParentViewController()
        view.removeFromSuperview()
    }

    override open func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
